//
//  DictionaryWriter.m
//  TWH-Asst3
//
//  Created by betopp on 3/23/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "DictionaryWriter.h"
#import "RootViewController.h"
#import "Note.h"
#import "NoteCategory.h"

@implementation DictionaryWriter

-(void)writeDictionaryFromRootView:(RootViewController*)rootview {
	
	if([rootview.categoriesArray count] <= 0)
		return;
	
	NSArray *docPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
	NSString *docDir = [docPaths objectAtIndex:0];	
	NSString *dicPath = [docDir stringByAppendingPathComponent:@"notesdic.plist"];

	//make dictionary, and array of categories
	NSDictionary *d = [[NSMutableDictionary alloc]init];
	NSMutableArray *cats = [[NSMutableArray alloc] initWithCapacity:20];
	[cats autorelease];
	[d autorelease];
	

	
	//build array of cats
	int c, n;
	for(c=0;c<[rootview.categoriesArray count];c++)
	{
		NoteCategory *notecategory = [rootview.categoriesArray objectAtIndex:c];
		
		//make dictionary, and array of notes
		NSDictionary *cat_dictionary = [[NSMutableDictionary alloc]init];
		NSMutableArray *notes = [[NSMutableArray alloc] initWithCapacity:20];
		[cat_dictionary autorelease];
		[notes autorelease];
		
		//store cat info
		[cat_dictionary setValue:notecategory.categoryName forKey:@"name"];
		[cat_dictionary setValue:[NSNumber numberWithInt:notecategory.categoryColor] forKey:@"color"];
		
		//build array of notes 
		for(n=0;n<[notecategory.notesArray count];n++)
		{
			Note *note = [notecategory.notesArray objectAtIndex:n];
		
			NSDictionary *note_dictionary = [[NSMutableDictionary alloc] init];
			[note_dictionary autorelease];
			
			[note_dictionary setValue:note.noteBody forKey:@"body"];
			[note_dictionary setValue:note.noteTitle forKey:@"title"];
			[note_dictionary setValue:[NSNumber numberWithInt:note.noteColor] forKey:@"color"];
			
			[notes addObject:note_dictionary];
		}
	
		//add array of notes
		[cat_dictionary setValue:notes forKey:@"notes"];
	
					
		[cats addObject:cat_dictionary];
	

	}
	
	//add array of cats
	[d setValue:cats forKey:@"categories"];
	
	[d writeToFile:dicPath atomically:NO];

}

-(void)readDictionaryToRootView:(RootViewController*)rootview {
	NSArray *docPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
	NSString *docDir = [docPaths objectAtIndex:0];	
	NSString *dicPath = [docDir stringByAppendingPathComponent:@"notesdic.plist"];
	
	NSDictionary *d = [[NSDictionary alloc]initWithContentsOfFile:dicPath];
	
	//pull cats
	NSArray *cats = [d valueForKey:@"categories"];
	
	NSLog(@"cats gotten");
	NSLog(@"%d",[cats count]);
	NSLog(@"\n");
	
	int c,n;
	for(c=0;c<[cats count];c++)
	{
		NSDictionary *cat_dictionary = [cats objectAtIndex:c];
		
		NSString *catname = [cat_dictionary objectForKey:@"name"];
		NSNumber *catcolor = [cat_dictionary objectForKey:@"color"];

		
		NoteCategory *newcat = [[NoteCategory alloc]init];
		newcat.categoryName = catname;
		newcat.categoryColor = [catcolor intValue];
		
		
		[rootview.categoriesArray addObject:newcat];
		
		NSArray *notes = [cat_dictionary objectForKey:@"notes"];
		
		for(n=0;n<[notes count];n++)
		{
			NSDictionary *note_dictionary = [notes objectAtIndex:n];
			
			NSString *notetitle = [note_dictionary objectForKey:@"title"];
			NSNumber *notecolor = [note_dictionary objectForKey:@"color"];
			NSString *notebody = [note_dictionary objectForKey:@"body"];
			
			
			Note *newnote = [[Note alloc]init];
			newnote.noteBody = notebody;
			newnote.noteTitle = notetitle;
			newnote.noteColor = [notecolor intValue];

			
			[newcat.notesArray addObject:newnote];
		}
	}
	
}


@end
